package com.zhentao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;

/**
 * AI消息实体类
 * 用于存储AI对话的详细消息记录
 */
@Document(collection = "ai_messages")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AiMessage {
    
    @Id
    private String id;
    
    /**
     * 用户ID
     */
    @Indexed
    @Field("userId")
    private Integer userId;
    
    /**
     * 会话ID - 关联到AiConversation
     */
    @Indexed
    @Field("conversationId")
    private String conversationId;
    
    /**
     * 消息内容
     */
    @Field("content")
    private String content;
    
    /**
     * 消息类型 - USER: 用户消息, ASSISTANT: AI回复
     */
    @Indexed
    @Field("messageType")
    private String messageType;
    
    /**
     * 消息时间戳
     */
    @Indexed
    @Field("timestamp")
    private LocalDateTime timestamp;
    
    /**
     * 消息序号 - 在会话中的顺序
     */
    @Field("sequenceNumber")
    private Integer sequenceNumber;
    
    /**
     * 消息状态 - SENT: 已发送, DELIVERED: 已送达, READ: 已读, FAILED: 发送失败
     */
    @Field("status")
    private String status = "SENT";
    
    /**
     * 是否为系统消息
     */
    @Field("isSystem")
    private Boolean isSystem = false;
    
    /**
     * 消息元数据 - 存储额外信息（JSON格式）
     */
    @Field("metadata")
    private String metadata;
    
    /**
     * AI模型信息 - 记录使用的AI模型
     */
    @Field("aiModel")
    private String aiModel;
    
    /**
     * 响应时间 - AI回复的响应时间（毫秒）
     */
    @Field("responseTime")
    private Long responseTime;
    
    /**
     * 错误信息 - 如果消息发送失败
     */
    @Field("errorMessage")
    private String errorMessage;
    
    /**
     * 构造函数 - 创建用户消息
     */
    public AiMessage(Integer userId, String conversationId, String content, String messageType) {
        this.userId = userId;
        this.conversationId = conversationId;
        this.content = content;
        this.messageType = messageType;
        this.timestamp = LocalDateTime.now();
        this.status = "SENT";
        this.isSystem = false;
    }
    
    /**
     * 构造函数 - 创建AI回复消息
     */
    public AiMessage(Integer userId, String conversationId, String content, String aiModel, Long responseTime) {
        this.userId = userId;
        this.conversationId = conversationId;
        this.content = content;
        this.messageType = "ASSISTANT";
        this.aiModel = aiModel;
        this.responseTime = responseTime;
        this.timestamp = LocalDateTime.now();
        this.status = "SENT";
        this.isSystem = false;
    }
    
    /**
     * 标记消息为已读
     */
    public void markAsRead() {
        this.status = "READ";
    }
    
    /**
     * 标记消息发送失败
     */
    public void markAsFailed(String errorMessage) {
        this.status = "FAILED";
        this.errorMessage = errorMessage;
    }
    
    /**
     * 判断是否为用户消息
     */
    public boolean isUserMessage() {
        return "USER".equals(this.messageType);
    }
    
    /**
     * 判断是否为AI回复
     */
    public boolean isAssistantMessage() {
        return "ASSISTANT".equals(this.messageType);
    }
}
